<route>
{
  "meta":{
    "title":"消息记录",
    "showMenu":true,
    "serialNum": 10,
    "localism": "sys.message_record"
  }
}
</route>
<template>
  <div>
    <el-row class="row-item" :gutter="12">
      <el-col :span="6">
        <el-input
          v-model="queryParams.templateCode"
          :placeholder="$t('sys.model_CODE')"
          size="small"
          clearable
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-select
          v-model="queryParams.type"
          style="width: 100%"
          :placeholder="$t('sys.template_type')">
          <el-option value="SYS" :label="$t('sys.system_message')"></el-option>
          <!--          <el-option value="WECHAT" label="企业微信"></el-option>-->
          <el-option value="EMAIL" :label="$t('sys.email')"></el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="queryParams.userName"
          :placeholder="$t('sys.recipient')"
          size="small"
          clearable
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="loadData(1)">{{$t('sys.query')}}</el-button>
        <el-button @click="onReset">{{$t('sys.reset')}}</el-button>
      </el-col>
    </el-row>

    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="templateCode" :label="$t('sys.model_CODE')"/>
      <el-table-column prop="toUserName" :label="$t('sys.recipient')"/>
      <el-table-column prop="createDate " :label="$t('sys.time')">
        <template slot-scope="scope">
          {{ scope.row.createDate | moment }}
        </template>
      </el-table-column>
      <el-table-column prop="actions" :label="$t('sys.operation')" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="$refs.messageDetailModal.show(scope.row)">{{$t('sys.details')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>
    <message-detail-modal ref="messageDetailModal" />
  </div>
</template>

<script>

import { getMsgThirdmessagePagelist } from '@/apis/paas/MsgThirdMessageController.js'
import { getMsgWebmessagePagelist } from '@/apis/paas/MsgWebMessageController.js'
import MessageDetailModal from './__components__/DetailModal'
export default {
  name: 'Message',
  components: {
    MessageDetailModal
  },
  data () {
    return {
      advanced: false,
      tableData: [],
      loading: false,
      queryParams: {
        type: 'SYS'
      },
      pageData: {
        pageSize: 10,
        pageNo: 1
      },
      total: 0,
      appList: []
    }
  },
  computed: {
    trimedQueryParams () {
      const params = Object.assign({}, this.queryParams)
      for (const prop in params) {
        if (params.hasOwnProperty(prop) && typeof params[prop] === 'string') {
          params[prop] = params[prop].trim()
        }
      }
      return params
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      this.loadData()
    },
    loadData (pageNo = this.pageData.pageNo) {
      let api = null

      if (this.queryParams.type === 'SYS') {
        api = getMsgWebmessagePagelist
      } else {
        api = getMsgThirdmessagePagelist
      }

      this.loading = true
      api({ ...this.queryParams,
        ...this.pageData,
        ...this.trimedQueryParams,
        pageNo }).then(res => {
        if (res.data.length === 0 && pageNo > 1) {
          this.loadData(pageNo - 1)
          return
        }
        this.tableData = res.data
        this.total = res.totalCount
        this.pageData = {
          pageSize: res.pageSize,
          pageNo: res.pageNo
        }
      }).finally(() => {
        this.loading = false
      })
    },

    onReset () {
      this.queryParams = {
        type: 'SYS'
      }
      this.loadData(1)
    }
  }
}
</script>

<style scoped>
.row-item .el-col  {
  margin-bottom: 10px;
}

</style>
